/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.manage.repository.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.customer.core.domain.CustomerTech;
import info.peigen.hotpot.business.customer.core.repository.entity.CustomerTechEntity;
import info.peigen.hotpot.business.customer.manage.convertor.CustomerTechManageConvertor;
import info.peigen.hotpot.business.customer.manage.repository.mapper.CustomerTechManageMapper;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlRepository;
import org.noear.solon.annotation.Component;

/**
 * <b>(CustomerTechManageRepository)</b>
 * 管理后台 "客户模块与技术相关的内容" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023-09-09
 */
@SuppressWarnings("unused")
@Component
public class CustomerTechManageRepository extends AbstractBeetlSqlRepository<CustomerTechManageMapper, CustomerTechEntity> {
    public CustomerTech active(String customerId) {
        Assert.isTrue(StrUtil.isNotEmpty(customerId), () -> new BusinessException(ResultCode.FAILURE, "激活时[customerId]不得为空!"));

        CustomerTechEntity entity = getMapper().findByCustomerId(customerId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!customerId=" + customerId));

        return CustomerTechManageConvertor.convertToDomain(entity);
    }

    public CustomerTech lock(String customerId) {
        Assert.isTrue(StrUtil.isNotEmpty(customerId), () -> new BusinessException(ResultCode.FAILURE, "加锁时[customerId]不得为空!"));

        CustomerTechEntity entity = getMapper().lockByCustomerId(customerId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!customerId为空!"));

        return CustomerTechManageConvertor.convertToDomain(entity);
    }

    public void removeByCustomerId(String customerId) {
        Assert.isTrue(StrUtil.isNotEmpty(customerId), () -> new BusinessException(ResultCode.FAILURE, "删除时[customerId]不得为空!"));

        getMapper().deleteByCustomerId(customerId);
    }

    public void store(CustomerTech domain) {
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));

        CustomerTechEntity entity = CustomerTechManageConvertor.convertToEntity(domain);

        saveOrUpdate(entity);
    }
}